package com.clps.algorithm03.链表;

public class DoubleNodeResult {
    private DoubleNode head;
    private DoubleNode tail;
    private int size;

    //头部添加
    private void addFirst(int val) {
        DoubleNode oldNode = head;
        DoubleNode newNode = new DoubleNode(val, null, oldNode);//创建心的头节点
        head = newNode;//更新头节点
        if (oldNode == null) {//如果之前按的链表为空，让tail也指向新的节点。
            tail = newNode;
        } else {
            oldNode.pre = newNode;//如果之前的链表不为空，让之前链表的头系欸但那的pre指向新的节点
        }
        size++;
    }


    //尾部节点
    private void addLast(int val) {
        DoubleNode oldNode = tail;
       DoubleNode newNode =  new DoubleNode(val,oldNode,null);//创建尾部节点
        tail = newNode;//更新尾部节点
        if(oldNode == null){ //如果之前链表为空，让head指向新的节点
            head = newNode;
        }else{
            oldNode.next = newNode;
        }
        size++;
    }


    //在指定位置添加节点
    public void  add(int index,int val){
        if(index<0 || index>size){
            throw  new IllegalStateException("越界了");
        }else if(index == size){
            addLast(val);
        }else{
            addBefore(val,findNode(index));
        }
    }

    private void addBefore(int val, Object node) {
    }

    DoubleNode  findNode(int index) {
        if(index<0 || index >=size){
            throw  new IllegalStateException("越界了。。。");
        }

        if(index<(size>>1)){
            DoubleNode x = head;
            for (int i = 0; i <index ; i++) {
                x=x.next;
            }
            return x;
        }else{//从后往前找
            DoubleNode x = tail;
            for (int i = size - 1; i > index; i--){
                x = x.pre;
            }
            return x;
        }
    }


    void  addBefore(int val,DoubleNode node){
        DoubleNode pre = node.pre;
        DoubleNode newNode = new DoubleNode(val, pre, node);
        node.pre = newNode;
        if(pre == null){
            head = newNode;
        }else{
            pre.next = newNode;
        }
        size++;
    }

    //删除头部的节点
    private  int removeFirst(){
        if(head == null){
            throw  new IllegalStateException("链表为空");
        }
        int val = head.val;//获取头部节点的值
        DoubleNode next = head.next;
        head.val = -1;
        head.next = null;
        head = next;//Gc
        if(next == null){
            tail = null;
        }else{
            head.pre = null;
        }
        size--;
        return  val;
    }


    //删除尾部节点
    public  int removeLast(){
        if(tail == null){
            throw  new IllegalStateException("链表为空");
        }
        int val = tail.val;//获取尾部节点的值
        DoubleNode pre = tail.pre;
        tail.pre = null;
        tail = pre;
        if(pre == null){
            head = null;
        }else{
            tail.next = null;
        }
        size--;
        return val;
    }

    //删除指定的节点
    int remove(DoubleNode x){
        int val = x.val;
        DoubleNode next = x.next;
        DoubleNode pre = x.pre;
        if(pre == null){
            head = next;
        }else{
            pre.next = next;
            x.pre = null;
        }

        if(next == null){
            tail = pre;
        }else {
            next.pre = pre;
            x.next  = null;
        }

        x.val = -1;
        size--;
        return  val;
    }


    //获取头部节点的值
    public int getFirst() throws NoSuchFieldException {
        if(head == null){
            throw  new NoSuchFieldException();
        }
        return  head.val;
    }

    //获取尾部的接地那
    public int getLase() throws NoSuchFieldException {
        if(tail == null){
            throw  new NoSuchFieldException();
        }
        return tail.val;
    }

}
